﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HDJ.Framework.Core
{
  public abstract  class TableTypeSupportBase
    {
        public  List<string> nullStringValue = new List<string> { "null","NULL" };

        public List<string> TypeCategory = new List<string>() { "base" };
        public abstract  List<string> TypeStringSupport
        {
            get;
        }


        public abstract Type TableType
        {
            get;
        }

        public abstract object GetTypeDefultValue();

        public string ObjectValue2TableString(object value)
        {
            if (value == null)
            {
                if (!TableType.IsValueType)
                {
                    return nullStringValue[0];
                }
                else
                {
                    return "";
                }
            }
               
            return Object2String(value);
        }
        protected virtual string Object2String(object value)
        {
            return value.ToString();
        }

        public  object TableStringData2Value(string data)
        {
            if (IsNull(data))
                return null;
            if (string.IsNullOrEmpty(data))
                return GetTypeDefultValue();
            if (TableType == typeof(string))
                return data;
            return String2Object(data);
        }
        protected virtual bool IsNull(string data)
        {
            if (!TableType.IsValueType&& nullStringValue.Contains(data))
                return true;
            return false;
        }
        
        protected virtual object String2Object(string data)
        {
            return Convert.ChangeType(data, TableType);
        }
    }
}
